package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.CustomException;
import com.reggie.common.R;
import com.reggie.entity.Category;
import com.reggie.entity.Dish;
import com.reggie.entity.Setmeal;
import com.reggie.mapper.CategoryMapper;
import com.reggie.service.CategoryService;
import com.reggie.service.DishService;
import com.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImp extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    public void safeRemove(Long ids) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapperDish = new LambdaQueryWrapper<>();
        lambdaQueryWrapperDish.eq(Dish::getCategoryId, ids);
        int count_dish = dishService.count(lambdaQueryWrapperDish);
        if(count_dish > 0) {
            // 报错
            throw new CustomException("该类别下还有其他菜品");
        }
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapperSetmeal = new LambdaQueryWrapper<>();
        lambdaQueryWrapperSetmeal.eq(Setmeal::getCategoryId, ids);
        int count_setmeal = setmealService.count(lambdaQueryWrapperSetmeal);
        if(count_setmeal > 0) {
            // 报错
            throw new CustomException("该类别下还有其他套餐");
        }
        this.removeById(ids);
    }
}
